import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    cart: []
  },
  mutations: {
    addToCart(state, product) {
      state.cart.push(product);
    },
    removeFromCart(state, product) {
      state.cart = state.cart.filter(item => item.id !== product.id);
    }
  },
  getters: {
    cartTotalPrice(state) {
      return state.cart.reduce((total, product) => total + product.price * product.quantity, 0);
    }
  }
});
